package cn.tedu.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author 作者:bjzhangjian
* @email 邮箱：bjzhangjian@tedu.cn
* @version 创建时间：2021年1月26日 下午5:38:33
* @description 描述：测试驱动加载
*   1、导包  mysql-connector-java-5.1.32.jar
*   2、创建TestJdbc类
*/
public class TestJdbc3 {
	public static void main(String[] args) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			//1、加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("驱动加载成功....");
			//2、建立连接localhost  127.0.0.1  192.168.43.233
			String url = "jdbc:mysql://localhost:3306/epidemic19?characterEncoding=utf8";
			String user = "root";//数据库的用户名
			String password = "root";//数据库的密码
			conn = DriverManager.getConnection(url, user, password);
			System.out.println(conn + "建立连接成功");
			//3、获取传输器对象  Statement
			st = conn.createStatement();
			//4、发送SQL
			String sql = "select * from province";
			//5、返回执行的结果结果executeUpdate 增删改  executeQuery 查询
			//ResultSet rs 结果集对象
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String provinceName = rs.getString("provinceName");
				int currentConfirmedCount = rs.getInt("currentConfirmedCount");
				int confirmedCount = rs.getInt("confirmedCount");
				int curedCount = rs.getInt("curedCount");
				int deadCount = rs.getInt("deadCount");
				System.out.println(provinceName + 
				",现存确诊：" + currentConfirmedCount + 
				",累计确诊：" + confirmedCount +
				",治愈人数：" + curedCount +
				",死亡人数：" + deadCount
				);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {//6、释放资源
			if (st != null) {
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					st = null;
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					conn = null;
				}
			}
			
		}
	}
	
}
